package com.spring.authManage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.spring.authManage.pojo.ResourceInfo;
import com.spring.authManage.pojo.ResourceInfoParam;
import com.spring.authManage.service.ResourceInfoService;
import com.spring.common.util.ChunQiuTool;

@Controller("resourceInfoController")
@RequestMapping("/resourceInfo")
public class ResourceInfoController {
	private Logger logger=Logger.getLogger(UserInfoController.class);


	@Autowired
	private ResourceInfoService resourceInfoService;

	/**
	 * 进入权限管理主页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toResourceInfoPage.do")
	public ModelAndView toResourceInfoPage(HttpServletRequest request,
			HttpServletResponse response, ResourceInfoParam param)
			throws Exception {
		// 内容初始化
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 父资源下拉框
		List<ResourceInfo> parentInfo = resourceInfoService.queryParentResource();
		// 进入页面初始化查询所有
		List<ResourceInfo> resourceInfoList = this.resourceInfoService.queryFirstLevel(param);
		// 返页面
		resultMap.put("resourceInfoList", resourceInfoList);
		resultMap.put("parentInfo", parentInfo);
		return new ModelAndView("/authManage/resourceManage", resultMap);
	}

	/**
	 * 获取权限信息列表
	 * 
	 * @param request
	 * @param response
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryResourceList.do")
	public ModelAndView queryResourceList(HttpServletRequest request,
			HttpServletResponse response, ResourceInfoParam param) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 父资源下拉框
		List<ResourceInfo> parentInfo = resourceInfoService.queryParentResource();
		// 进入页面初始化查询所有
		List<ResourceInfo> resourceInfoList = this.resourceInfoService.queryResourceByParam(param);
		resultMap.put("resourceInfoList", resourceInfoList);
		resultMap.put("parentInfo", parentInfo);
		return new ModelAndView("/authManage/resourceManage", resultMap);
	}

	/**
	 * 根据已显示的一级菜单查询并显示二级菜单
	 * 
	 * @param request
	 * @param response
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/querySubResource.do")
	public ModelAndView querySubResource(HttpServletRequest request,
			HttpServletResponse response, ResourceInfoParam param)
			throws Exception {
		String firstId = param.getId();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 根据获得的一级菜单ID查询所有二级菜单信息
		List<ResourceInfo> resourceInfoList = this.resourceInfoService.querySecondLevel(firstId);
		resultMap.put("resourceInfoList", resourceInfoList);
		return new ModelAndView("/authManage/resourceDetail", resultMap);
	}

	/**
	 * 添加权限初始化 载入父资源选项下拉内容
	 * 
	 * @param request
	 * @param response
	 * @param info
	 * @throws Exception
	 */
	@RequestMapping("/toAddResourcePage.do")
	public ModelAndView toAddResourcePage(HttpServletRequest request,
			HttpServletResponse response, ResourceInfo info) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 父资源下拉框
		List<ResourceInfo> parentInfo = resourceInfoService.queryAllParentResource();
		resultMap.put("parentInfo", parentInfo);
		return new ModelAndView("/authManage/resourceEdit", resultMap);
	}

	/**
	 * 添加权限
	 * 
	 * @param request
	 * @param response
	 * @param info
	 * @throws Exception
	 */
	@RequestMapping("/addResource.do")
	public ModelAndView addResource(HttpServletRequest request,HttpServletResponse response, ResourceInfo info) throws Exception {

		String operator = ChunQiuTool.getUserName();
		
		info.setUpdateUser(operator);
		String result = this.resourceInfoService.addResource(info);
		// resultMap用来装message返回成功参数至前台
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (result.equals("Y")) {
			resultMap.put("message", "success");
			logger.info(ChunQiuTool.getUserName()+"insert"+ info.getName() + info.getUrl());
		} else {
			resultMap.put("message", result);
		} 
		// 父资源下拉框
		List<ResourceInfo> parentInfo = resourceInfoService.queryAllParentResource();
		resultMap.put("parentInfo", parentInfo);
		return new ModelAndView("/authManage/resourceEdit",resultMap);
	}

	/**
	 * 根据ID获取权限信息
	 * 
	 * @param request
	 * @param response
	 * @param resourceId
	 */
	@RequestMapping("/queryResourceById.do")
	public void queryResourceById(HttpServletRequest request,
			HttpServletResponse response, String resourceId) {

	}
	/**
	 * 更新权限信息 初始化
	 * 
	 * @param request
	 * @param response
	 * @param info
	 * @throws Exception
	 */
	@RequestMapping("/toUpdateResourcePage.do")
	public ModelAndView toUpdateResourcePage(HttpServletRequest request,
			HttpServletResponse response, ResourceInfo info) throws Exception {
		String res_id = info.getId();
		info = this.resourceInfoService.queryResourceById(res_id);
		// resultMap用来装message返回成功参数至前台
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 父资源下拉框
		List<ResourceInfo> parentInfo = resourceInfoService.queryAllParentResource();
		resultMap.put("parentInfo", parentInfo);
		resultMap.put("initUpdate", info);
		return new ModelAndView("/authManage/resourceUpdate", resultMap);
	}
	/**
	 * 更新权限信息
	 * 
	 * @param request
	 * @param response
	 * @param info
	 * @throws Exception
	 */
	@RequestMapping("/updateResource.do")
	public ModelAndView updateResource(HttpServletRequest request,
			HttpServletResponse response, ResourceInfo info) throws Exception {
		String operator = ChunQiuTool.getUserName();
		info.setUpdateUser(operator);
		String result = this.resourceInfoService.updateResource(info);
		// resultMap用来装message返回成功参数至前台
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (result.equals("Y")) {
			resultMap.put("message", "success");
			logger.info(ChunQiuTool.getUserName()+"update"+ info.getName() + info.getUrl());
		} else {
			resultMap.put("message", result);
		} 
		// 重新获取id
		String res_id = info.getId();
		// 更新资源初始化查询 传至页面
		info = this.resourceInfoService.queryResourceById(res_id);
		resultMap.put("initUpdate", info);
		// 父资源下拉框
		List<ResourceInfo> parentInfo = resourceInfoService.queryAllParentResource();
		resultMap.put("parentInfo", parentInfo);

		return new ModelAndView("/authManage/resourceUpdate",resultMap);
	}

	/**
	 * 禁用资源
	 * 
	 * @param request
	 * @param response
	 * @param info
	 * @throws Exception
	 */
	@RequestMapping("/deleteResource.do")
	public ModelAndView deleteResource(HttpServletRequest request,
			HttpServletResponse response, ResourceInfo info) throws Exception {
		String res_id = info.getId();
		// 作废资源
		this.resourceInfoService.deleteResource(res_id);
		logger.info(ChunQiuTool.getUserName()+"delete"+ info.getName() + info.getUrl());
		return null;
	}
	/**
	 * 重启资源
	 * 
	 * @param request
	 * @param response
	 * @param info
	 * @throws Exception
	 */
	@RequestMapping("/restartResource.do")
	public ModelAndView restartResource(HttpServletRequest request,
			HttpServletResponse response, ResourceInfo info) throws Exception {
		String res_id = info.getId();
		// 重启资源
		this.resourceInfoService.restartResourceInfo(res_id);
		logger.info(ChunQiuTool.getUserName()+"restart"+ info.getName() + info.getUrl());
		return null;

	}
	
}
